package com.zqxx.examine.management.controller.action.admin;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zqxx.examine.management.security.StandardPasswordEncoderForSha1;
import com.zqxx.examine.management.security.UserInfo;
import com.zqxx.examine.management.service.UserService;
import com.zqxx.examine.model.exam.Message;
import com.zqxx.examine.model.user.User;

@Controller
public class UserActionAdmin
{
  @Autowired
  private UserService userService;
  
  @RequestMapping(value={"/admin/add-user-{authority}-{groupId}"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
  @ResponseBody
  public Message addUser(@RequestBody User user, @PathVariable String authority, @PathVariable Integer groupId)
  {
    UserInfo userInfo = (UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    user.setCreateTime(new Date());
    String password = user.getPassword() + "{" + user.getUserName().toLowerCase() + "}";
    PasswordEncoder passwordEncoder = new StandardPasswordEncoderForSha1();
    String resultPassword = passwordEncoder.encode(password);
    user.setPassword(resultPassword);
    user.setEnabled(true);
    user.setCreateBy(userInfo.getUserid());
    user.setUserName(user.getUserName().toLowerCase());
    Message message = new Message();
    try
    {
      this.userService.addUser(user, authority, groupId.intValue(), userInfo.getRoleMap());
    }
    catch (Exception e)
    {
      if (e.getMessage().contains(user.getUserName()))
      {
        message.setResult("duplicate-username");
        message.setMessageInfo("重复的用户名");
      }
      else if (e.getMessage().contains(user.getNationalId()))
      {
        message.setResult("duplicate-national-id");
        message.setMessageInfo("重复的身份证");
      }
      else if (e.getMessage().contains(user.getEmail()))
      {
        message.setResult("duplicate-email");
        message.setMessageInfo("重复的邮箱");
      }
      else if (e.getMessage().contains(user.getPhoneNum()))
      {
        message.setResult("duplicate-phone");
        message.setMessageInfo("重复的电话");
      }
      else
      {
        message.setResult(e.getCause().getMessage());
        e.printStackTrace();
      }
    }
    return message;
  }
  
  @RequestMapping(value={"/admin/update-teacher"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
  @ResponseBody
  public Message updateTeacher(@RequestBody User user)
  {
    UserInfo userInfo = (UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    user.setCreateTime(new Date());
    String password = user.getPassword() + "{" + user.getUserName() + "}";
    PasswordEncoder passwordEncoder = new StandardPasswordEncoderForSha1();
    String resultPassword = "";
    if (user.getPassword() != null) {
      resultPassword = "".equals(user.getPassword().trim()) ? "" : passwordEncoder.encode(password);
    }
    user.setPassword(resultPassword);
    user.setEnabled(true);
    Message message = new Message();
    try
    {
      this.userService.updateUser(user, null);
    }
    catch (Exception e)
    {
      if (e.getMessage().contains(user.getUserName()))
      {
        message.setResult("duplicate-username");
        message.setMessageInfo("重复的用户名");
      }
      else if (e.getMessage().contains(user.getNationalId()))
      {
        message.setResult("duplicate-national-id");
        message.setMessageInfo("重复的身份证");
      }
      else if (e.getMessage().contains(user.getEmail()))
      {
        message.setResult("duplicate-email");
        message.setMessageInfo("重复的邮箱");
      }
      else if (e.getMessage().contains(user.getPhoneNum()))
      {
        message.setResult("duplicate-phone");
        message.setMessageInfo("重复的电话");
      }
      else
      {
        message.setResult(e.getCause().getMessage());
        e.printStackTrace();
      }
    }
    return message;
  }
  
  @RequestMapping(value={"/admin/change-teacher-status-{userId}-{enabled}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  @ResponseBody
  public Message changeTeacherStatus(Model model, @PathVariable("userId") int userId, @PathVariable("enabled") boolean enabled)
  {
    List<Integer> idList = new ArrayList();
    idList.add(Integer.valueOf(userId));
    this.userService.changeUserStatus(idList, enabled);
    return new Message();
  }
}
